package com.vsoontech.audiorecorder;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.AsyncTask;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

class BeepManagerPro {
        // 音频文件bi.pcm是44100采样率，16bit，单声道
        private static final String BI_PCM = "/data/test.pcm";
        private static final int SAMPLE_RATE = 44100;

        private AudioTrack mAudioTrack;
        private PlayWavTask playWavTask;
        private boolean isPlaying = false;
        private boolean isInit = false;

        public BeepManagerPro() {
            int bufferSize = AudioTrack.getMinBufferSize(SAMPLE_RATE,
                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
                    AudioFormat.ENCODING_PCM_16BIT);
            mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
                    SAMPLE_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO,
                    AudioFormat.ENCODING_PCM_16BIT,
                    bufferSize, AudioTrack.MODE_STREAM);
            playWavTask = new PlayWavTask();
        }

        public void init() {
            if (isInit) {
                Log.w("BeepManager", "isInit, return");
                return;
            }
            Log.d("BeepManager", "init mAudioTrack");
            //开始播放
            mAudioTrack.play();
            isInit = true;
        }

        public void play() {
            if (isPlaying) {
                Log.w("BeepManager", "isPlaying, return");
                return;
            }
            if (!isInit) {
                init();
            }
            isPlaying = true;
            playWavTask.execute();
            Log.d("BeepManager", "playWavTask start");
        }

        public void release() {
            Log.d("BeepManager", "release");
            isInit = false;
            if (mAudioTrack != null) {
                mAudioTrack.release();
                mAudioTrack = null;
            }
            isPlaying = false;
            if (playWavTask != null) {
                playWavTask.cancel(true);
                playWavTask = null;
            }
        }

        private class PlayWavTask extends AsyncTask<Void, Integer, Void> {
            @Override
            protected Void doInBackground(Void... arg0) {
                byte[] buffer = new byte[1024];
                DataInputStream dis = null;
                try {
                    //AssetManager assetManager = MainApplication.getInstance().getAssets();
                    File file = new File(BI_PCM);
                    InputStream inputStream = new FileInputStream(file);
                    //定义输入流，将音频写入到AudioTrack类中，实现播放
                    dis = new DataInputStream(new BufferedInputStream(inputStream));
                    Log.d("BeepManager", "play start");
                    //由于AudioTrack播放的是流，所以，我们需要一边播放一边读取
                    while (dis.available() > 0) {
                        int i = 0;
                        while (dis.available() > 0 && i < buffer.length) {
                            buffer[i] = dis.readByte();
                            i++;
                        }
                        //然后将数据写入到AudioTrack中
                        mAudioTrack.write(buffer, 0, buffer.length);
                    }
                } catch (Exception e) {
                    Log.e("BeepManager", "PlayWavTask Exception: " + e.getMessage());
                } finally {
                    try {
                        if (dis != null) {
                            dis.close();
                        }
                        if (mAudioTrack != null) {
                            mAudioTrack.release();
                            mAudioTrack = null;
                        }
                    } catch (Exception e) {
                        Log.e("BeepManager", "PlayWavTask colse Exception: " + e.getMessage());
                    }
                }
                isInit = false;
                isPlaying = false;
                playWavTask = null;
                Log.d("BeepManager", "play done");
                return null;
            }
        }
    }